package cs2340.quarterblack.wheresmystuff;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SettingsPage extends Activity {
	
	Context context;
	
	Button btnAccept;
	Button btnCancel;
	TextView etUserId;
	EditText etPassword;
	EditText etConfirm;
	EditText etMail;
	TextView tvOutput;
	
	Account account;
	int failedAttempts;
	boolean loginSuccessful;

	/**
	 * creates the login page
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		
		// Initialize UI elements
		btnAccept = (Button)findViewById(R.id.st_btn_accept);
		btnCancel = (Button)findViewById(R.id.st_btn_cancel);
		tvOutput = (TextView)findViewById(R.id.st_pwOut);
		
		etMail = (EditText)findViewById(R.id.st_email);
		etUserId = (TextView)findViewById(R.id.st_UserId);
		etPassword = (EditText)findViewById(R.id.st_password);
		etConfirm = (EditText)findViewById(R.id.st_confirm);
		account = Model.getAccount();
		failedAttempts = 0;
		etMail.setText(account.getEmail());
		etUserId.setText(account.getUserId());
		UpdateM m = new UpdateM();
		etConfirm.addTextChangedListener(m);
		
		/**
		 * sets up the accept button
		 */
		btnAccept.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				String mail = etMail.getText().toString();
				String pw = etPassword.getText().toString();
				String cpw = etConfirm.getText().toString();
				if(pw.length() == 0 || pw.equals(cpw))
				{	
					if(mail.length() !=0 && mail.contains("@") == true)
					{
						account.setEmail(etMail.getText().toString());
						if(pw.length() != 0)
						{
							account.setPw(pw);
						}
						onBackPressed();
					}
					else
					{
						Toast.makeText(getApplicationContext(), "email not valid", 5).show();
					}
				}
				else
				{
					Toast.makeText(getApplicationContext(), "Passwords don't match", 5).show();
				}
			}
		});
		
		/**
		 * this sets the cancel button
		 */
		btnCancel.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}
	private class UpdateM implements TextWatcher
	{

		public UpdateM()
		{
		}
		/**
		 * copies the data from text field 1 to text field 2
		 */
		@Override
		public void afterTextChanged(Editable arg0) {

		}
		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			if(etPassword.getText().toString().equals( etConfirm.getText().toString()) == false)
			{
				tvOutput.setTextColor(getResources().getColor(R.color.RED));
				tvOutput.setText("Passwords do not match");
			}
			else
			{
				tvOutput.setTextColor(getResources().getColor(R.color.GREEN));
				tvOutput.setText("Passwords match");
			}
			
		}
		
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}

}
